package cn.genmer.test.security.netty.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program:  文件复制
 * @description:
 * @author: Genmer
 * @create: 2021-09-04 22:15
 **/
public class Channel4 {
    public static void main(String[] args) throws IOException {


        String string1 = "/Users/genmersmbp/Documents/Codes/tmp/channel1.txt";
        String string2 = "/Users/genmersmbp/Documents/Codes/tmp/copy2.txt";
        // 创建一个输出流
        FileInputStream fileInputStream = new FileInputStream(string1);
        FileOutputStream fileOutputStream = new FileOutputStream(string2);

        // 获得channel - 如果channel实例为空，会实例化一个FileChannelImpl - 方法内部一个synchronized的大括号
        FileChannel readChannel = fileInputStream.getChannel();
        FileChannel writeChannel = fileOutputStream.getChannel();

        // 使用Channel的transferFrom方法 来完成文件的拷贝
        writeChannel.transferFrom(readChannel,0, readChannel.size());


        fileInputStream.close();
        fileOutputStream.close();
    }
}
